package com.atguigu.create.singleton.type7;

/**
 * 单例模式-静态内部类.推荐
 */
public class SingletonTest7 {
    public static void main(String[] args) {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1==instance2);
        System.out.println("instance1:"+instance1.hashCode());
        System.out.println("instance2:"+instance2.hashCode());
    }
}

class Singleton{
    // 1. 私有化构造器
    private Singleton() {
    }

    // 2. 静态内部类
    private static class SingletonSource{
         private static final Singleton SINGLETON =new Singleton();
    }

    // 3. getInstance
    public static Singleton getInstance(){
        return SingletonSource.SINGLETON;
    }

}
